﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.IO;
using Portal.Systems.Model;
using Portal.Database.Utility;
using Portal.Database.Service;
using Portal.Web.Utility;
using Portal.Core;
using Portal.Systems.Bussiness.Template;

namespace Portal.Systems.Bussiness
{
    public class PageZoneProcessBll 
    {
        public static PageZoneCls[] ReadingPageZone(string SiteId, string PageId, string Keyword)
        {
            IDatabaseService DBService = WebDatabaseService.CreateDBService(SiteId);
            SiteParam OSiteParam = WebEnvironments.CreateSiteParam(SiteId);
            string DataLanguage = WebConfig.GetDataLanguage(OSiteParam);
            DataSet dsResult =
                    DBService.GetDataSet(null, "select * from TablePageZone where frkPageId=@PageId and AssetCode=@AssetCode order by SortIndex",
                    new DbParam[]{
                        new DbParam("AssetCode",OSiteParam.SiteCode),
                        new DbParam("PageId",PageId)
                    });
            if (dsResult.Tables[0].Rows.Count ==  0)
            {
                return new PageZoneCls[0];
            }
            PageZoneCls[] CmsPageZones = PageZoneParser.ParseFromDataTable(dsResult.Tables[0]);
            dsResult.Clear();
            dsResult.Dispose();
            return CmsPageZones;
        }


        public static PageZoneCls[] ReadingPageZoneWithModules(
            string SiteId, 
            string PageId)
        {
            IDatabaseService DBService = WebDatabaseService.CreateDBService(SiteId);
            SiteParam OSiteParam = WebEnvironments.CreateSiteParam(SiteId);
            string DataLanguage = WebConfig.GetDataLanguage(OSiteParam);
            DataSet dsResult =
                    DBService.GetDataSet(null, "select * from TablePageZone where frkPageId=@PageId and AssetCode=@AssetCode order by SortIndex",
                    new DbParam[]{
                        new DbParam("AssetCode",OSiteParam.SiteCode),
                        new DbParam("PageId",PageId)
                    });
            if (dsResult.Tables[0].Rows.Count == 0)
            {
                return new PageZoneCls[0];
            }
            PageZoneCls[] CmsPageZones = PageZoneParser.ParseFromDataTable(dsResult.Tables[0]);
            for (int iIndex = 0; iIndex < CmsPageZones.Length; iIndex++)
            {
                PageZoneModuleCls[] PageZoneModules = PageZoneModuleProcessBll.ReadingPageZoneModule(SiteId, CmsPageZones[iIndex].PageZoneId);
                for (int iIndexModule = 0; iIndexModule < PageZoneModules.Length; iIndexModule++)
                {
                    CmsPageZones[iIndex].PageZoneModules.Add(PageZoneModules[iIndexModule]);
                }
            }
            dsResult.Clear();
            dsResult.Dispose();
            return CmsPageZones;
        }

        public static PageZoneCls CreatePageZoneModel(string SiteId, string PageZoneId)
        {
            IDatabaseService DBService = WebDatabaseService.CreateDBService(SiteId);
            DataSet dsResult =
                    DBService.GetDataSet(null, "select * from TablePageZone where (PageZoneId=@PageZoneId or ZoneAlias=@PageZoneId) ",
                    new DbParam[] 
                    { 
                        new DbParam("PageZoneId", PageZoneId),
                    });
            if (dsResult.Tables[0].Rows.Count == 0)
            {
                return null;
            }
            PageZoneCls OPageZone = PageZoneParser.ParseFromDataRow(dsResult.Tables[0].Rows[0]);
            dsResult.Clear();
            dsResult.Dispose();
            return OPageZone;
        }



        public static void AddPageZone(string SiteId, PageZoneCls OPageZone)
        {
            IDatabaseService DBService = WebDatabaseService.CreateDBService(SiteId);
            SiteParam OSiteParam = WebEnvironments.CreateSiteParam(SiteId);
            string DataLanguage = WebConfig.GetDataLanguage(OSiteParam);
            if (string.IsNullOrEmpty(OPageZone.PageZoneId))
            {
                OPageZone.PageZoneId = System.Guid.NewGuid().ToString();
            }
            DBService.Insert(null, "TablePageZone",
                    new DbParam[] 
                    { 
                        new DbParam("PageZoneId", OPageZone.PageZoneId),
                        new DbParam("ZoneAlias",OPageZone.ZoneAlias),
                        new DbParam("ZoneName",OPageZone.ZoneName),
                        new DbParam("frkPageId",OPageZone.PageId),
                        new DbParam("AssetCode",OSiteParam.SiteCode),
                        new DbParam("DataLanguage",DataLanguage),
                        new DbParam("Active",OPageZone.Active),
                        new DbParam("SortIndex",OPageZone.SortIndex),
                    });
        }


        public static void DeletePageZone(string SiteId, string PageZoneId)
        {
            IDatabaseService DBService = WebDatabaseService.CreateDBService(SiteId);
            string Query =
                " Delete from TablePageZoneModule where frkPageZoneId=@PageZoneId; "+
                " Delete from TablePageZone where PageZoneId=@PageZoneId ";
            DBService.ExecuteNonQuery(null, Query,
                    new DbParam[] 
                    { 
                        new DbParam("PageZoneId", PageZoneId),
                    });
        }


        public static void SavePageZone(string SiteId,string PageZoneId, PageZoneCls OPageZone)
        {
            IDatabaseService DBService = WebDatabaseService.CreateDBService(SiteId);
            SiteParam OSiteParam = WebEnvironments.CreateSiteParam(SiteId);
            string DataLanguage = WebConfig.GetDataLanguage(OSiteParam);
            DBService.Update(null, "TablePageZone","PageZoneId",PageZoneId,
                    new DbParam[] 
                    { 
                        new DbParam("ZoneAlias",OPageZone.ZoneAlias),
                        new DbParam("ZoneName",OPageZone.ZoneName),
                        new DbParam("Active",OPageZone.Active),
                        new DbParam("SortIndex",OPageZone.SortIndex),
                    });
        }
    }
}

